<?php
class Macys_Api_V2_Catalog_Category  {

	private static $m_pInstance;
	protected $data;
	private $_client;
	private $_api;
	private $_api_uri;
	private $log;

	protected $endpoint = "/v2/catalog/category";

	// Will be added as per request
	private $currentUri = null;
	private $currentParams = array();
	private $allowedParams = array (
							"categoryHierarchyDepth",
							"resultsPerPage",
							"currentPage",
							"includeRefinements",
							"sortBy",
							"sortOrder",
							"imageQuality",
							"imageWidth",
	"includeRefinements"
	);
	private $_categoryId;
	private $_data = null;
	private $categoryNavigation = array();


	private function __construct($config = null){
		$this->log = Zend_Registry::get('log');
		$this->_client = new Macys_Base_HttpClient();
		//$this->_api = new Macys_Base_Session();
		$this->_api = Macys_Base_Config::getInstance();
		$key = "";
		if($config == null) {
			$key = $this->_api->getApiKey('v2catalogcategory');
			$this->_api_uri = $this->_api->getUri();
		}
		else {
			$key = $config['api_key'];
			$this->_api_uri = $config['api_uri'];
		}
		$this->_client->setClientHeader($key);
		$this->_client->setMethod("GET");
	}


	public function getInstance() {
		if (!self::$m_pInstance){
			self::$m_pInstance = new self();
		}
		return self::$m_pInstance;
	}





	public function run() {
		if(isset($this->_categoryId)) {
			$this->_prepareUri();
			$this->_client->setUri($this->currentUri);
			$this->_client->setAcceptType();
			$this->_client->request();
			$this->_data = $this->_client->getResponseBodyArray();
		}
	}

	public function clearParams() {
		$this->currentParams = array();
	}


	public function processInputParams($postParams) {

		unset($postParams['submit']);
		foreach ($postParams as $a=>$b) {
			if($b == "" || $b == null)
			unset($postParams[$a]);
		}

		foreach ($postParams as $c=>$d) {
			$this->log->debug("Input params : " . $c);
			if(in_array($c, array('controller', "action", "module")))
			unset($postParams[$c]);
		}
		// Set Category If available
		if(isset($postParams['categoryId'])){
			$this->setCategoryId($postParams['categoryId']);
			unset($postParams['categoryId']);
		}

		if(is_array($postParams) && count($postParams) > 0) {
			foreach ($postParams as $k=>$value) {
				if(in_array($k, $this->allowedParams)) {
					$n = ucfirst($k);
					$funct = 'set'.$n;
					try{
						self::getInstance()->{$funct}($value);
						unset($postParams[$k]);
					}
					catch (Exception $e) {
						$this->log->crit("Exception in processInputParams : " . $e->getMessage());
						$this->log->crit("Exception in processInputParams : " . $e->getTraceAsString());
					}
					//call_user_func($this->{$funct}, array($k,$value));

				}
			}
		}
		if(is_array($postParams) && count($postParams) > 0){
			foreach ($postParams as $a=>$b) {
				$this->log->info("Adding refinements : " . $a . " = " . $b);
				$this->__setParams($a, $b);
				unset($postParams[$a]);
			}
		}
		
	}



	public function getResponse() {
		if(empty($this->_data)) {
			return null;
		}
		return new Macys_Api_V2_Catalog_Category_Response($this->_data, $this->currentParams);
	}

	public function getRawData() {
		return $this->_data;
	}


	public function getCategoryId() {
		return $this->_categoryId;
	}

	public function setCategoryId($id) {
		if(is_nan($id)) {
			throw new Macys_Api_V2_Catalog_Exception('Category ID Should be a Numeric Value');
		}
		$this->_categoryId = $id;
		return $this;
	}

	public function getAdditionalParams() {}
	public function setAdditionalParams() {}
	public function getImageWidth() {
		return $this->_getParams('imageWidth');
	}
	public function setImageWidth($width) {
		if(is_nan($width)) {
			throw new Macys_Api_V2_Catalog_Exception('Width Should be a Numeric Number');
		}
		$this->_setParams('imageWidth', $width);
		return $this;
	}

	public function getImageQuality() {
		return $this->_getParams('imageQuality');
	}
	public function setImageQuality($quality) {
		if(is_nan($quality)) {
			throw new Macys_Api_V2_Catalog_Exception('CurrentPage Should be a Numeric Number');
		}
		$this->_setParams('imageQuality', $quality);
		return $this;
	}

	public function getSortOrder() {}
	
	public function setSortOrder($sortOrder) {
		if(!in_array($sortOrder, array('ASC', 'DESC'))){
			throw new Macys_Api_V2_Catalog_Exception('SortBy Should be a Proper Value, Invalid Value : ' . $sortOrder);
		}
		$this->_setParams('sortOrder', $sortOrder);
		return $this;
	}

	public function getSortBy() {
		return $this->_getParams('sortBy');
	}
	public function setSortBy($sortBy) {
		if(!in_array($sortBy, array('PRICE', 'CUSTOMER_RATING', 'NAME', 'CREATED'))){
			throw new Macys_Api_V2_Catalog_Exception('SortBy Should be a Proper Value, Invalid Value : ' . $refinement);
		}
		$this->_setParams('sortBy', $sortBy);
		return $this;
	}


	public function getIncludeRefinements() {
		return $this->_getParams('includeRefinements');
	}

	public function setIncludeRefinements($refinement) {
		if(!in_array($refinement, array('YES', 'ONLY', 'NO'))){
			throw new Macys_Api_V2_Catalog_Exception('includeRefinements Should be a Proper Value, Invalid Value : ' . $refinement);
		}
		$this->_setParams('includeRefinements', $refinement);
		return $this;
	}

	public function getCurrentPage() {
		return $this->_getParams('currentPage');
	}

	public function setCurrentPage($page) {
		if(is_nan($page)) {
			throw new Macys_Api_V2_Catalog_Exception('CurrentPage Should be a Numeric Number');
		}
		$this->_setParams('currentPage', $page);
		return $this;
	}

	public function getResultsPerPage() {
		return $this->_getParams('resultsPerPage');
	}

	public function setResultsPerPage($number){
		if(is_nan($number)) {
			throw new Macys_Api_V2_Catalog_Exception('ResultsPerPage Should be a Numeric Number');
		}
		$this->_setParams('resultsPerPage', $number);
		return $this;
	}

	public function getCategoryHierarchyDepth() {
		return $this->_getParams('categoryHierarchyDepth');
	}

	public function setCategoryHierarchyDepth($depth){
		if(is_nan($depth)) {
			throw new Macys_Api_V2_Catalog_Exception('CategoryHierarchyDepth Should be a Numeric Number');
		}
		$this->_setParams('categoryHierarchyDepth', $depth);
		return $this;
	}

	private function _setParams($name, $value) {
		if(in_array($name, $this->allowedParams)) {
			//$this->currentParams = array_merge($this->currentParams, array($name => $value));
			$this->__setParams($name, $value);
		}
	}

	private function __setParams($name,$value) {
		$this->currentParams = array_merge($this->currentParams, array($name => $value));
	}

	private function _getParams($name) {
		if(!in_array($name, $this->allowedParams)) {
			throw new Macys_Api_V2_Catalog_Exception('This Parameter is not a allowed params : ' . $name);
		}
		if(array_key_exists($name, $this->currentParams)) {
			return $this->currentParams[$name];
		}
		return null;
	}

	private function _prepareUri(){
		$params = http_build_query($this->currentParams, null, '&');
		$this->currentUri = $this->_api_uri . $this->endpoint . "/" . $this->_categoryId . "?". $params;
	}


	public function getStaticInfo() {
		//return new Zend_Navigation_Page_Mvc($this->getCategories());
		return new Macys_Api_V2_Catalog_Category_StaticInfo($this->_api->getSite());
		//return $this->getCategories();
	}


}